package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.Dept;
import com.atguigu.springcloud.request.SaveDeptRequest;
import com.atguigu.springcloud.request.UpdateDeptCountRequest;
import com.atguigu.springcloud.response.BaseResponse;
import com.atguigu.springcloud.response.MultiResponse;
import com.atguigu.springcloud.response.OneResponse;
import com.atguigu.springcloud.service.DeptService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by luojie on 2018/9/6.
 */
@RestController("deptController")
@RequestMapping(value = "/dept")
@Api(tags = "部门信息接口")
@CrossOrigin //跨域访问 swagger 要使用到
public class DeptController extends BaseController{

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private DeptService deptService;

    @ApiOperation("新增部门")
    @PostMapping(value = "/save")
    @ApiResponses({@ApiResponse(code = 200, message = "保存成功"),
            @ApiResponse(code = 400, message = "错误的请求")})
    public BaseResponse save(@ApiParam @RequestBody @Validated SaveDeptRequest request, HttpServletResponse response){
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        Dept deptDB = deptService.findByDname(request.getDname());
        if(deptDB != null){
           return new BaseResponse(HttpStatus.BAD_REQUEST.value(), "该部门已存在");
        }
        logger.info("保存部门 [uuid:{}, request:{}]", uuid, request);
        Dept dept = deptService.save(request);
        logger.info("保存部门 ，成功[uuid:{}, request:{}]", uuid, request);
        return successSaveBaseResponse();
    }


    @ApiOperation("根据ID查询该部门的信息详情")
    @GetMapping("/detail/{id}")
    @ApiResponses({@ApiResponse(code = 990018, message = "部门不存在"), @ApiResponse(code = 200, message = "查询成功")})
    public
    OneResponse<Dept> detail(@ApiParam("部门id") @PathVariable(name = "id") @NotBlank String id) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        logger.info("根据ID查询该部门的信息详情开始[uuid:{}, id:{}]", uuid, id);
        Dept dept = deptService.findById(id);

        logger.info("根据ID查询该部门的信息详情成功[uuid:{}, id:{}]", uuid, id);
        return successQueryOneResponse(dept);
    }

    @ApiOperation("查询所有部门的信息")
    @GetMapping("/list")
    @ApiResponses({@ApiResponse(code = 990018, message = "部门不存在"), @ApiResponse(code = 200, message = "查询成功")})
    public MultiResponse<Dept> list() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        logger.info("查询所有部门的信息开始[uuid:{}]", uuid);
        List<Dept> list = new ArrayList<>();
        list = deptService.findAll();
        logger.info("查询所有部门的信息成功[uuid:{}]", uuid);
        return successQueryMultiResponse(list);
    }

    @ApiOperation("修改部门人数")
    @PostMapping("/updateCount")
    @ApiResponses({@ApiResponse(code = 990018, message = "部门不存在"), @ApiResponse(code = 200, message = "查询成功")})
    public
    BaseResponse updateCount(@ApiParam @RequestBody @Validated UpdateDeptCountRequest request) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        logger.info("修改部门人数 [uuid:{}, request:{}]", uuid, request);
        Dept dept = deptService.findById(request.getId());
        if(dept == null){
            return new BaseResponse(990018, "部门不存在");
        }
        dept = deptService.updateCount(dept, request.getCount());
        logger.info("修改部门人数成功[uuid:{}, request:{}]", uuid, request);
        return successQueryOneResponse(dept);
    }

}
